﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Data.SqlClient;
public partial class Deparment_Worker_Meeting : System.Web.UI.Page
{
    private string strTitle, strContent, strDate;
    private long strAccountid;
    private bool isMust;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumDeparment);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            strAccountid = _Account.AccountId;

            if (!IsPostBack)
            {
                txtContent.Text = DateTime.Now.ToShortDateString();
            }
            else
            {
                isMust = checkValue(Request);
                Save();
            }

        }
    }


    /// <summary>
    /// 保存信息
    /// </summary>
    private void Save()
    {
        if (isMust)
        {
            string uploadFile = "";  //附件文件名标题
            string uploadFilePath = "";   //附件存储路径
            string downLoadUrl = "";   //下载地址

            if (FileUpload1.HasFile)
            {
                uploadFile = FileUpload1.FileName;
                DateTime dtime = DateTime.Now;
                string filename = dtime.ToShortDateString().Replace("-", "") +
                     "-" + dtime.Hour.ToString() + dtime.Minute.ToString() + dtime.Second.ToString() +
                     "-" + new Random().Next(10000).ToString() + new System.IO.FileInfo(uploadFile).Extension;
                uploadFilePath = System.Web.HttpContext.Current.Request.MapPath("../DupdateFile/") + filename;
                downLoadUrl = "../DupdateFile/" + filename;
                FileUpload1.SaveAs(uploadFilePath);
            }

            int succ = 0;
            string procName = "P_DMeeting_Add";
            SqlParameter[] para = new SqlParameter[7];
            para[0] = new SqlParameter("@title", strTitle);
            para[1] = new SqlParameter("@content", strContent);
            para[2] = new SqlParameter("@adddate", strDate);
            para[3] = new SqlParameter("@accountid", strAccountid);
            para[4] = new SqlParameter("@uploadfile", uploadFile);
            para[5] = new SqlParameter("@uploadfilepath", downLoadUrl);
            para[6] = new SqlParameter("@succ", succ);
            para[6].Direction = ParameterDirection.Output;
            Db db = new Db();
            db.ExecuteProcedure(procName, para);
            db.Close();
            succ = (int)para[6].Value;
            if (succ == 1)
            {
                Response.Write("<script>alert('记录保存成功!');self.close();</script>");
            }
            else
            {
                Response.Write("<script>alert('记录保存失败!')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('请将记录填写完整!!!')</script>");
        }
    }

    /// <summary>
    /// 验证必填项是否有空
    /// </summary>
    /// <returns></returns>
    private bool checkValue(HttpRequest request)
    {
        strTitle = request.Form["txtName"].Trim();
        strContent = txtContent.Text;
        strDate = txtAddDate.Text.Trim();

        if (strTitle == "" || strContent == "" || strDate == "")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
